package com.doublefish.doublefishrobot.ui.setting;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.doublefish.doublefishrobot.BuildConfig;
import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.base.BaseActivity;
import com.doublefish.doublefishrobot.ui.setting.model.AppInfoBean;
import com.doublefish.doublefishrobot.ui.setting.service.AppInfoService;
import com.doublefish.doublefishrobot.utils.VersionCompareUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者:dugaofeng on 2024/10/17 08:31
 */
public class AppVersionActivity extends BaseActivity {

    @BindView( R.id.tv_title)
    TextView titleTv;

    @BindView( R.id.tv_cur_version)
    TextView curVersionTv;

    @BindView( R.id.ll_new_version)
    LinearLayout newVersionLl;

    @BindView( R.id.tv_new_version)
    TextView newVersionTv;

    @BindView( R.id.tv_btn)
    TextView appBtn;


    private String curVersion;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_app_version;
    }

    @Override
    protected void initView() {
        titleTv.setText(R.string.app_version_2);

        newVersionLl.setVisibility(View.INVISIBLE);
        appBtn.setVisibility(View.INVISIBLE);
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        curVersion = intent.getStringExtra("version");
        curVersionTv.setText(curVersion);

        getVersion();
    }

    @Override
    protected void initListener() {
    }

    private void getVersion() {
        // client  平台 int 1.荣耀、小米、OPPO、vivo  2.百度 3.腾讯 4.360 5.华为  10.蒲公英   99.iOS

        int client = BuildConfig.ENV_TYPE;
        AppInfoService.getVersion(this, client, new AppInfoService.OnAppInfoServiceCallback() {
            @Override
            public void onNetworkPermissionError() {
                Log.i("onNetworkPermission", "xxx");
                showToast(getString(R.string.network_error));
            }

            @Override
            public void onSuccess(AppInfoBean bean) {
                if (BuildConfig.DEBUG) {
                    appBtn.setVisibility(View.VISIBLE);
                    return;
                }
                if (VersionCompareUtils.compare(curVersion, bean.getVer()) == 1) {
                    appBtn.setVisibility(View.VISIBLE);
                    newVersionLl.setVisibility(View.VISIBLE);
                    newVersionTv.setText(bean.getVer());
                }
            }

            @Override
            public void onFailure(String err) {
                Log.i("onFailure", err);
                showToast(err);
            }
        });
    }

    @OnClick(R.id.ll_back)
    public void onBackBtnClick() {
        finish();
    }

    @OnClick(R.id.tv_btn)
    public void onAppBtnClick() {
        // client  平台 int int 1.荣耀、小米、OPPO、vivo  2.百度 3.腾讯 4.360  5.华为 10.11.蒲公英   99.iOS
        int client = BuildConfig.ENV_TYPE;
        toUpdateApp(client);
    }

    private void toUpdateApp(int client) {
        //client 平台 int 1.荣耀、小米、OPPO、vivo  2.百度 3.腾讯 4.360  5.华为  10.11.蒲公英   99.iOS
//        com.tencent.android.qqdownloader 腾讯应用宝
//        com.qihoo.appstore 360手机助手
//        com.baidu.appsearch 百度手机助手

        switch (client) {
            case 1: {
                toEachMarket();
            }
            break;
            case 2: {
                toBaiduMarket();
            }
            break;
            case 3: {
                toTengxunMarket();
            }
            break;
            case 4: {
                to360Market();
            }
            break;
            case 5: {
                toHuaweiMarket();
            }
            break;

            case 10: {
                toPgy(false);
            }
            case 11: {
                toPgy(true);
            }
            break;

            default:
                break;
        }
    }

    private boolean hasApp(Context context, String marketPackageName) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo pkgInfo = packageManager.getPackageInfo(marketPackageName, 0);
            if (null != pkgInfo && null != pkgInfo.applicationInfo && pkgInfo.applicationInfo.enabled){
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    private void toEachMarket() {

        String[] packages = {
                "com.huawei.appmarket",     //华为
                "com.hihonor.appmarket",    //荣耀
                "com.xiaomi.market",        //小米
                "com.oppo.market",          //oppo
                "com.heytap.market",        //oppo 欢太
                "com.bbk.appstore"          //vivo
        };

        for (String packageName:packages) {
            if (hasApp(this, packageName)) {
                toAppMarket(packageName);
                return;
            }
        }
        //没有找到上面的商店
        toOnelinkMarket();
    }


    private void toOnelinkMarket() {
        //这个onelink 是荣耀、小米、oppo、vivo通用的
        String url = "https://m.malink.cn/s/NRZR7j";
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        startActivity(intent);
    }

    //前往各大应用市场市场,根据特定的包名
    private void toAppMarket(String marketPackageName) {
        try {
            String packageName = "com.doublefish.u8";
            String scheme = "market://details?id=";
            Uri uri = Uri.parse(scheme + packageName);

            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            if (marketPackageName != null) {
                intent.setPackage(marketPackageName);
            }
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);

        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void toPgy(boolean test) {
        String url;
        if (test) {
            url = "https://www.pgyer.com/CtwzdXAZ";
        } else {
            url = "https://www.pgyer.com/yz3Z6D1o";
        }

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        startActivity(intent);
    }

    private void toHuaweiMarket() {
        toAppMarket("com.huawei.appmarket");
    }
    private void toBaiduMarket() {
        toAppMarket("com.baidu.appsearch");
    }

    private void toTengxunMarket() {
        toAppMarket("com.tencent.android.qqdownloader");
    }

    private void to360Market() {
        toAppMarket("com.qihoo.appstore");
    }
}
